import re

## 根据一个io.file，修改另一个io.file的坐标
## 修改坐标的规则：x坐标不变，y坐标=21.888-(41.952-x坐标)
## 原始path : /projects/cascade/workspace/xwqiu/DDRPHY/innovus.250423.CM_LOGIC_H.V/scripts

def get_offset(myfile):
	re_list = []
	with open(myfile, 'r') as f:
		all_lines = f.readlines()
		for line in all_lines:
			a = re.findall(r"\d*\.\d+\b", line)
			print(a)
			re_list.append(a)
	return re_list

def fix_pin_xy(a, b):
	re_list = []
	for i in range(len(a)):
		if a[i]==[] or b[i]==[]:
			re_list.append(0)
		elif len(a[i])!=3 or len(b[i])!=3:
			re_list.append(0)
		else:
			x1 = float(a[i][0])
			x2 = float(b[i][0])
			y1 = round(21.888-(41.952-x2), 4)
			re_list.append(y1)
	return re_list
		
def write_file(myfile, re_list):
	with open(myfile, "r") as f:
		lines = f.readlines()
	
	for i in range(len(lines)):
		a = re.sub(r"\d*\.\d+\b", str(re_list[i]), lines[i], count=1)
		lines[i] = a
		print(a)
	with open("./new", "w") as f:
		f.writelines(lines)
		

a = "/projects/cascade/workspace/xwqiu/DDRPHY/innovus.250423.CM_LOGIC_H.V/io.file"
b = "/projects/cascade/workspace/xwqiu/DDRPHY/innovus.DDR_WRITE_READ_H.V/io.file.top"

re_a = get_offset(a)
re_b = get_offset(b)

c = fix_pin_xy(re_a, re_b)

write_file(a, c)